package com.homework08;

import com.homework08.entity.User;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.homework08.Database.Database;

public class TobeMap {
    public static void main(String[] args) {
        toMap(Database.listSet());
    }
    private static void toMap(Set<User> users) {
        Map<String, Set<User>> map = new HashMap<>();
        for(User u : users) {
            map.computeIfAbsent(u.getCity(), k -> new HashSet<>());
            map.get(u.getCity()).add(u);
        }
        map.forEach((k,v) -> {
            System.out.println("key = 城市:" + k);
            for(User user : v) {
                System.out.print( "  value = ID:" + user.getId() + "   ");
                System.out.println("姓名:" + user.getName() + "   ");
            }
        });
        /*for(Map.Entry<String, Set<User>> entry : map.entrySet() ) {
            System.out.println("key = 城市:" + entry.getKey());
            for(User u : entry.getValue()) {
                System.out.print( "  value = ID:" + u.getId() + "   ");
                System.out.println("姓名:" + u.getName() + "   ");
            }
        }*/
    }
}
